package edu.wpi.cs509.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import edu.wpi.cs509.representation.TeamColor;
import edu.wpi.cs509.ui.images.StrategoPieceImage;


/**
 * The GraveyardGrave is the graphics JPanel that paints the Stratego piece
 * image in the graveyard.
 * 
 * @author afoltan
 *
 */
@SuppressWarnings("serial")
public class GraveyardGrave extends JPanel {

	private static final int BORDER = 4;
	
	// a reference to the Stratego piece images 
	private StrategoPieceImage image;
	
	// the player color for selecting the piece to draw
	private TeamColor teamColor;

	/**
	 * The constructor for a grave in the graveyard.
	 * 
	 * A grave is the graphic image of the stratego piece
	 * 
	 * @param image - the Stratego image, which contains both red and blue graphic images
	 * @param TeamColor - the color of this grave
	 */
	public GraveyardGrave( StrategoPieceImage image, TeamColor teamColor ) {
		this.image = image;
		this.teamColor = teamColor;

		// set the border - needed for highlighting during mouse selection
		setBorder( BorderFactory.createEmptyBorder(BORDER,BORDER,BORDER,BORDER) );

		// calculate the grave size
		int w = image.getWidth() + BORDER + BORDER;
		int h = image.getHeight() + BORDER + BORDER;
		
		// the preferred size is derived from the image size
		Dimension dim = new Dimension( w, h );
		setPreferredSize(dim);
		
		// show it
		setVisible(true);
	}
	
	/**
	 * Repaint the grave
	 * 
	 * This method recalculates the width and height based on the parent container
	 * size.  It will not maintain the aspect ratio of the image.
	 * 
	 * @param g - the graphics object used to redraw this panel
	 */
    public void paintComponent(Graphics g) {
   	
    	Insets inset = getInsets();
    	int w = getWidth() - inset.left - inset.right;
    	int h = getHeight() - inset.top - inset.bottom;
    	    	
    	// redraw the image in the new dimension
    	g.drawImage(image.getScaledImage(teamColor, new Dimension( w,h )), inset.left, inset.top, null);				
	}
	
}

